﻿using MediatR;
using Order.Api.Read.Application.Command;
using Order.Api.Read.Dto.Order;
using Order.Api.Read.Service.RegistrationOrderService;
using Order.ErrorCode;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Order.Api.Read.Application.Handler
{
    /// <summary>
    /// 挂号订单分页处理器
    /// </summary>
    public class RegistrationOrderPageHandler : IRequestHandler<RegistrationOrderPageCommand, ApiPageIng<RegistrationOrderPageDto>>
    {
        private readonly IRegistrationOrderService _registrationOrderService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="registrationOrderService">挂号订单服务</param>
        public RegistrationOrderPageHandler(IRegistrationOrderService registrationOrderService)
        {
            _registrationOrderService = registrationOrderService ?? throw new ArgumentNullException(nameof(registrationOrderService), "挂号订单服务不能为空");
        }

        /// <summary>
        /// 处理挂号订单分页查询请求
        /// </summary>
        /// <param name="request">分页查询命令</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>分页结果</returns>
        public async Task<ApiPageIng<RegistrationOrderPageDto>> Handle(RegistrationOrderPageCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 参数验证
                if (request == null)
                {
                    throw new ArgumentNullException(nameof(request), "分页查询请求不能为空");
                }

                // 调用服务层获取分页数据
                var result = await _registrationOrderService.GetRegistrationOrderPageAsync(request);
                return result;
            }
            catch (Exception ex)
            {
                // 记录异常日志
                // TODO: 添加日志记录
                throw new Exception($"处理挂号订单分页查询请求时发生错误: {ex.Message}", ex);
            }
        }
    }

    /// <summary>
    /// 挂号订单详情处理器
    /// </summary>
    public class RegistrationOrderDetailHandler : IRequestHandler<RegistrationOrderDetailCommand, RegistrationOrderDetailDto>
    {
        private readonly IRegistrationOrderService _registrationOrderService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="registrationOrderService">挂号订单服务</param>
        public RegistrationOrderDetailHandler(IRegistrationOrderService registrationOrderService)
        {
            _registrationOrderService = registrationOrderService ?? throw new ArgumentNullException(nameof(registrationOrderService), "挂号订单服务不能为空");
        }

        /// <summary>
        /// 处理挂号订单详情查询请求
        /// </summary>
        /// <param name="request">详情查询命令</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>订单详情</returns>
        public async Task<RegistrationOrderDetailDto> Handle(RegistrationOrderDetailCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 参数验证
                if (request == null)
                {
                    throw new ArgumentNullException(nameof(request), "详情查询请求不能为空");
                }

                // 调用服务层获取详情数据
                var result = await _registrationOrderService.GetRegistrationOrderDetailAsync(request);
                return result;
            }
            catch (Exception ex)
            {
                // 记录异常日志
                // TODO: 添加日志记录
                throw new Exception($"处理挂号订单详情查询请求时发生错误: {ex.Message}", ex);
            }
        }
    }
}
